package br.com.portal.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import br.com.portal.beans.LinhaPedido;
import br.com.portal.dao.LinhaPedidoDAO;

@WebServlet("/PreencherLinha")
public class PreencherLinha extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public PreencherLinha() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String codigo = request.getParameter("id");
		
		Gson gson = new Gson(); 
		PrintWriter resposta = response.getWriter();
		
		try {
			LinhaPedido linha = LinhaPedidoDAO.preencherLinha(codigo);
			String ret = "{success: true, items: " + gson.toJson(linha) + " }";
			resposta.print(ret);
		} catch (Exception e) {
			resposta.print("{success: false}");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}